#!coding: utf-8
# Create your views here.
import math
import time
import os
import base64
import hashlib 


from exceptions import Exception



def check_session(appkey, appsecret, parameters, session, sign):
    src=appkey+parameters+session+appsecret 
    m=hashlib.md5()
    m.update(src)
    if m.digest() != base64.b64decode(sign):
        raise Exception( (10076, 'check session key and parameters fail: [appkey:%s sessionKey:%s parameters:%s sign:%s]' 
                %(appkey, session, parameters, sign)))
    parameters = base64.b64decode(parameters)
    param={}
    for i in parameters.split('&'):
        (k, v) = i.split('=')
        param[k]=v
    nick = param['visitor_nick'].decode('gbk').strip()
    if not nick:
        raise Exception((10076, 'get nick fail, need redirect to taobao to login'))
    param['nick']=nick
    param['top_session']=session
    param['top_parameters']=parameters
    param['top_sign']=sign 
    param['top_appkey'] = appkey
    return param

